//
//  UserInfoLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/22.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "UserInfoLogic.h"
#import "userInfoModel.h"
#import "OperationDataBaseModel.h"

@interface UserInfoLogic()
{
    OperationDataBaseModel * operation;
}

@end

@implementation UserInfoLogic

#pragma mark ----------------插入用户信息
-(void)insertUserInfo:(userInfoModel *)userModel
{
    NSMutableDictionary * staffDict = [self modelConvertDict:userModel];
    operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"userInfo" withDict:staffDict];
}


#pragma mark -----------------查询客户信息
-(void)queryUserInfo:(userInfoDataBlock)userModel
{
    __block userInfoModel * Model=[[userInfoModel alloc]init];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"userInfo" withCondition:@"" block:^(FMResultSet *set) {
        while ([set next]) {
            Model.uid=[set stringForColumn:@"id"];
            Model.name=[set stringForColumn:@"name"];
            Model.mobile=[set stringForColumn:@"mobile"];
            Model.gender=[set stringForColumn:@"gender"];
            Model.identityId=[set stringForColumn:@"identityId"];
            Model.phone=[set stringForColumn:@"phone"];
            Model.address=[set stringForColumn:@"address"];
            Model.email=[set stringForColumn:@"email"];
            Model.certification=[set stringForColumn:@"certification"];
            Model.headUrl=[set stringForColumn:@"headUrl"];
            Model.areaCode = [set stringForColumn:@"areaCode"];
        }
        if (userModel) {
            userModel(Model);
        }
    }];
}

/**
 *  模型转化为字典
 *
 *  @param model 组客户模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(userInfoModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:[self getStr:model.uid] forKey:@"id"];
    [staffDict setValue:model.mobile forKey:@"mobile"];
    [staffDict setValue:model.email forKey:@"email"];
    [staffDict setValue:model.name forKey:@"name"];
    [staffDict setValue:model.gender forKey:@"gender"];
    [staffDict setValue:model.identityId forKey:@"certification"];
    [staffDict setValue:model.address forKey:@"address"];
    [staffDict setValue:model.headUrl forKey:@"headUrl"];
    [staffDict setValue:model.guesture forKey:@"guesture"];
    [staffDict setValue:model.areaCode forKey:@"areaCode"];
    return staffDict;
}


#pragma mark -----删除表
-(void)deleteTable
{
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteTable:@"userInfo"];
}


-(NSString *)getStr:(NSString *)str
{
    NSString *returnStr=@"";
    if (str==nil || [@"" isEqualToString:str]==YES || str.length==0 ) {
        returnStr=@"";
    }
    else
    {
        returnStr=str;
    }
    return returnStr;
}


@end
